
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
import Layout from "/@/layout/index.vue";

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'RedirectC',
        component: () => import('/@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: "/login",
    name: "Login",
    hidden: true,
    component: () => import("/@/views/login/index.vue"),
    meta: {
      title: '登录',
      icon: "el-icon-s-home",
      noCache: true,
      roles: ['admin'],
    },
  },

  {
    path: '/404',
    component: () => import('/@/views/error-page/404.vue'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('/@/views/error-page/401.vue'),
    hidden: true,
  },
]
export const asyncRoutes = [
  {
    path: "/",
    component: Layout,
    name: 'Dashboard',
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: 'Dashboard',
        component: () => import("/@/views/dashboard/index.vue"),
        meta: {
          title: '首页',
          icon: "dashboard",
          noCache: true,
          roles: ['admin'],
        },
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    name: "User",
    redirect: "/user",
    children: [
      {
        path: "user",
        name: "User",
        component: () => import("/@/views/user/index.vue"),
        meta: {
          title: '用户',
          icon: "users",
          noCache: true,
          roles: ['admin'],
        },
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    name: "Project",
    redirect: "/project",
    children: [
      {
        path: "project",
        name: "Project",
        component: () => import("/@/views/project/index.vue"),
        meta: {
          title: '项目',
          icon: "list",
          noCache: true,
          roles: ['admin'],
        },
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    name: "Task",
    redirect: "/task",
    children: [
      {
        path: "task",
        name: "Task",
        component: () => import("/@/views/task/index.vue"),
        meta: {
          title: '任务',
          icon: "tree",
          noCache: true,
          roles: ['admin'],
        },
      },
    ],
  },
  {
    path: "/admin",
    name: "Admin",
    component: Layout,
    redirect: "/admin/api",
    meta: {
      title: '管理员',
      icon: "component",
      noCache: true,
      roles: ['admin'],
    },
    children: [
      {
        path: "group",
        name: "Group",
        component: () => import("/@/views/admin/group/index.vue"),
        meta: {
          title: '分组管理',
          icon: "banner",
          noCache: true,
          roles: ['admin'],
        },
      },
      {
        path: "api",
        name: "Api",
        component: () => import("/@/views/admin/api/index.vue"),
        meta: {
          title: 'api管理',
          icon: "banner",
          noCache: true,
          roles: ['admin'],
        },
      },
      {
        path: "menu",
        name: "Menu",
        component: () => import("/@/views/admin/menu/index.vue"),
        meta: {
          title: '导航管理',
          icon: "banner",
          noCache: true,
          roles: ['admin'],
        },
      },
    ],
  },

  { path: '/:pathMatch(.*)', redirect: '/404', hidden: true }
]

const createRouters = createRouter({
  history: createWebHistory('/'), // base 当成参数传递了
  routes: constantRoutes.concat(asyncRoutes),
  scrollBehavior: () => ({ left: 0, top: 0 }), //  return 期望滚动到哪个的位置
})

//
const router = createRouters

export function resetRouter() {
  const WHITE_NAME_LIST = [
    'Login',
  ]
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !WHITE_NAME_LIST.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export default router
